from chapterlib import chapter_generator
import re

template_text = '''☀️心满意足餐厅☀️

📆{date}

🍼每日灵奶:
🍉{dujing1}
🍇{dujing2}

🍱每日配菜:
🌿{daogao}
🌿选取当日读的章节中有感动的经文DG

🕊️自由选择两项搭配🕊️

🍷营养丰富，请勿错过🍷'''


class DailyDujingDaogaoText:

    def __init__(self, dujing_1, dujing_2, daogao_info, share_date):

        self.__template__ = template_text
        self.dujing_1 = dujing_1
        self.dujing_2 = dujing_2
        self.daogao_info = daogao_info
        self.share_date = share_date

    @property
    def template(self):
        return self.__template__

    @template.setter
    def template(self, text):
        self.__template__ = text

    def get_text(self):
        template = self.template

        newlines = []
        daogao_text = ''
        pattern = r'(.*){daogao}(.*)'

        if m := re.search(pattern, template, re.MULTILINE):
            prefix = m.group(1)
            suffix = m.group(2)
            template = re.sub(pattern, '{daogao}', template)

            for line in self.daogao_info:
                newlines.append(prefix + line.strip() + suffix)

            daogao_text = '\n'.join(newlines)

        return template.format(date=self.get_date_info(), dujing1=self.dujing_1, dujing2=self.dujing_2, daogao=daogao_text)

    def get_week_info(self):
        week_dict = {1: '周一', 2: '周二', 3: '周三',
                     4: '周四', 5: '周五', 6: '周六', 7: '周日'}
        return week_dict.get(self.share_date.isoweekday())

    def get_date_info(self):
        return f'{self.share_date.month:02}月{self.share_date.day:02}日 {self.get_week_info()}'

    def generate_html(self):
        return '<div class="col-md-4 shadow-lg p-3 mb-5 bg-white rounded"><p>'+'<br>'.join(self.get_text().splitlines())+'<br></p><button type="button" class="btn btn-primary btn-lg btn-block" onclick="copy_text(this)">点击复制文本</button></div>'
